package com.example.filestoragedemo;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    EditText editFileName,editContent;
    Button btnInnerWrite,btnOuterWrite,btnInnerRead,btnOuterRead;

    /*动态申请SDCard读写权限的代码段开始*/
    private static final int REQUEST_EXTERNAL_STORAGE=1;
    private static String[] PERMISSIONS_STORAGE={
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};
    //验证SDCard读写权限的方法veryfyStoragePermission(this)
    public static void verifyStoragePermissions(AppCompatActivity activity){
        //检查是否已经具有权限
        int permission= ActivityCompat.checkSelfPermission(activity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
        //如果没有权限，则弹出对话框向用户申请权限
        if(permission!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(
                    activity,PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
        }
    }
    /*动态申请SDCard读写权限的代码段结束*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        verifyStoragePermissions(this);           //执行验证SDCard读写权限的方法

        editFileName=(EditText) findViewById(R.id.editFileName);
        editContent=(EditText) findViewById(R.id.editContent);
        btnInnerWrite=(Button) findViewById(R.id.btnInnerWrite);
        btnOuterWrite=(Button) findViewById(R.id.btnOuterWrite);
        btnInnerRead=(Button) findViewById(R.id.btnInnerRead);
        btnOuterRead=(Button) findViewById(R.id.btnOuterRead);
        btnInnerWrite.setOnClickListener(this);
        btnOuterWrite.setOnClickListener(this);
        btnInnerRead.setOnClickListener(this);
        btnOuterRead.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        String fileName=editFileName.getText().toString();
        String content=editContent.getText().toString();
        FileOutputStream fos=null;
        FileInputStream fis=null;
        switch(view.getId()){
            case R.id.btnInnerWrite:       // 写入内部存储文件
                try {
                    fos= openFileOutput(fileName,MODE_PRIVATE);
                    fos.write(content.getBytes());
                    Toast.makeText(this,"写入内部存储文件"+fileName+"成功",Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    try {
                        if(fos!=null){
                            fos.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.btnOuterWrite:       //写入外部存储文件
                String state = Environment.getExternalStorageState();
                if(state.equals(Environment.MEDIA_MOUNTED)){
                    File SDPath=Environment.getExternalStorageDirectory();
                    File file=new File(SDPath,fileName);
                    try {
                        fos=new FileOutputStream(file);
                        fos.write(content.getBytes());
                        Toast.makeText(this,"写入外部存储文件"+fileName+"成功",Toast.LENGTH_LONG).show();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            if(fos!=null){
                                fos.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                break;
            case R.id.btnInnerRead:       //读取内部存储文件
                content="";
                try {
                    fis=openFileInput(fileName);
                    byte[] buffer=new byte[fis.available()];
                    fis.read(buffer);
                    content=new String(buffer);
                    Toast.makeText(this,"读取内部存储文件"+fileName+"成功："+content,Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    try {
                        if(fis!=null){
                            fis.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.btnOuterRead:       //读取外部存储文件
                String state1=Environment.getExternalStorageState();
                if(state1.equals(Environment.MEDIA_MOUNTED)){
                    File SDPath=Environment.getExternalStorageDirectory();
                    File file=new File(SDPath,fileName);
                    try {
                        fis=new FileInputStream(file);
                        byte[] buffer=new byte[fis.available()];
                        fis.read(buffer);
                        content=new String(buffer);
                        Toast.makeText(this,"读取外部存储文件"+fileName+"成功："+content,Toast.LENGTH_LONG).show();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            if(fis!=null){
                                fis.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                break;
        }
    }
}
